﻿using DrCarDrive.DBService;
using DrCarDrive.Util;
using DrCarDrive.Wpf.WpfWindow;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DrCarDrive.Wpf.WpfPage
{
    /// <summary>
    /// Interaction logic for FuelTopUp.xaml
    /// </summary>
    public partial class MedicalExpenses : Page
    {
        private static MedicalExpenses instance;
        private ExpensesService expenseService;
        public MedicalExpenses()
        {
            InitializeComponent();
        }

        public static object Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new MedicalExpenses();
                }

                return instance;
            }
        }

        private void btnExpenseCancel_Click(object sender, RoutedEventArgs e)
        {
            txtAmount.Text = "";
            dtpExpenseDateTime.Text = "";
            txtRemark.Text = "";
            cmbExpensesType.SelectedIndex = -1;
        }

        private void btnExpenseSave_Click(object sender, RoutedEventArgs e)
        {
            if (validate() == true)
            {
                tbl_expenses expense = new tbl_expenses();
                expense.ID = GUIDGenerator.expensesKey();
                DateTime expenseDateTime = DateTime.ParseExact(dtpExpenseDateTime.Text, "yyyy-MM-dd HH:mm:ss", System.Globalization.CultureInfo.CurrentCulture);
                expense.ADD_DATE = expenseDateTime;

                decimal amount;
                Decimal.TryParse(txtAmount.Text, out amount);
                expense.AMOUNT = amount;

                expense.FKY_DRIVE = SessionData.drive.ID;
                expense.REMARK = txtRemark.Text;
                expense.FKY_EXPENSE = cmbExpensesType.SelectedValue.ToString();

                expenseService = new ExpensesService();
                
                int status = expenseService.save(expense);
                if (status > 0)
                {
                    MSGWindow success = new MSGWindow(Messages.TTL_EXPENSE, Messages.MSG_EXPENSE_ADD);
                    success.ShowDialog();
                    instance = null;
                    MainWindow.Instance.ContentFrame.Content = DashboardPage.Instance;
                }
            }
            else
            {
                MSGWindow errorWindow = new MSGWindow(Messages.TTL_FILL_REQUIRED, Messages.MSG_FILL_REQUIRED_INFORMATION);
                errorWindow.ShowDialog();
            }

        }

        private Boolean validate()
        {
            if (txtAmount.Text == "" || dtpExpenseDateTime.Value == null || cmbExpensesType.SelectedIndex==-1)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        public static void destroy()
        {
            instance = null;
        }

        private void Page_Loaded_1(object sender, RoutedEventArgs e)
        {
            ExpensesDescriptionService expDescService = new ExpensesDescriptionService();
            List<tbl_expense_desc> expTypeList = expDescService.viewAll();

            cmbExpensesType.ItemsSource = expTypeList;
            cmbExpensesType.DisplayMemberPath ="DESCRIPTION" ;
            cmbExpensesType.SelectedValuePath = "ID";
        }

        private void cmbExpensesType_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            SelectExpensesTextBlock.Visibility = cmbExpensesType.SelectedItem == null ? Visibility.Visible : Visibility.Hidden;
        }
    }
}
